import orgTable from '@/views/organize/orgTable/orgTable.vue';
import SelectCity from '@/util/provinces.vue';
import Cookies from 'js-cookie';
import util from '@/libs/util';

export default {
    components: {
        orgTable, SelectCity
    },
    data () {
        return {
            baseuser: JSON.parse(Cookies.get('baseUser')),
            query: {},
            options: {
                shortcuts: [{
                    text: '今天',
                    value () {
                        return new Date();
                    }
                }]
            },
            cityinfo: {
                province: '',
                city: '',
                block: ''
            },
            quertCity: {
                province: '',
                city: '',
                block: ''
            },
            orgTree: [],
            orgselect: ''
        };
    },
    mounted () {
        this.loadTree();
    },
    methods: {
        loadTree () {
            let roles = this.baseuser.roles;
            var id = this.baseuser.depId;
            var type = this.baseuser.type;
            if(type==1){
                util.ajax.post('/base/organize/findOrgAll', util.toFormData({
                    orgid: ''
                })).then(res => {
                    if (res.data.data) {
                        let tree = [];
                        var dataList = res.data.data;
                        if (this.baseuser.type === 2) {
                            for (var i in dataList) {
                                let item = dataList[i];
                                if (item.id == this.baseuser.orgId) {
                                    tree.push({
                                        id: item.id,
                                        title: item.name,
                                        expand: true,
                                        children: []
                                    });
                                }
                            }
                        } else {
                            for (var i in dataList) {
                                let item = dataList[i];
                                if (item.parentid == 0) {
                                    tree.push({
                                        id: item.id,
                                        title: item.name,
                                        expand: true,
                                        children: []
                                    });
                                }
                            }
                        }
                        if (tree.length > 0) {
                            this.createTree(tree, dataList);
                        }
                        this.orgTree = tree;
                    }
                });
            }else{
                for (let i in roles) {
                    let item = roles[i];
                    if (item.name.indexOf('管理员') > 0) {
                        id = this.baseuser.orgId;
                    }
                }
                util.ajax.post('/base/organize/getDepByOrgId' /* '/base/organize/getDepByOrgId' */, util.toFormData({
                    id: id
                })).then(res => {
                    if (res.data.data) {
                        let tree = [];
                        var dataList = res.data.data;
                        if (this.baseuser.type === 2) {
                            for (var i in dataList) {
                                let item = dataList[i];
                                if (item.id == this.baseuser.orgId) {
                                    tree.push({
                                        id: item.id,
                                        title: item.name,
                                        expand: true,
                                        children: []
                                    });
                                }
                            }
                        } else {
                            for (var i in dataList) {
                                let item = dataList[i];
                                if (item.parentid == 0) {
                                    tree.push({
                                        id: item.id,
                                        title: item.name,
                                        expand: true,
                                        children: []
                                    });
                                }
                            }
                        }
                        if (tree.length > 0) {
                            this.createTree(tree, dataList);
                        }
                        this.orgTree = tree;
                    }
                });
            }
        },
        createTree (tree, data) {
            let childTree = [];
            let _this = this;
            tree.forEach(treeItem => {
                childTree = [];
                data.forEach(dataItem => {
                    if (treeItem.id === dataItem.parentid) {
                        childTree.push({
                            id: dataItem.id,
                            title: dataItem.name,
                            expand: true,
                            children: []
                        });
                    }
                });
                if (childTree.length > 0) {
                    treeItem.children = childTree;
                    _this.createTree(childTree, data);
                }
            });
        },
        treeSelect (res) {
            if (res.length !== 0) {
                this.orgselect = res[0].id;
            } else {
                this.orgselect = '';
            }
            let _this = this;
            this.$refs.orgtable.$nextTick(() => {
                _this.$refs.orgtable.loadList();
            });
        },
        loadList () {
            /*this.cityinfo = {
                province: this.$refs.quertCity.province,
                city: this.$refs.quertCity.city,
                block: this.$refs.quertCity.block
            };*/
            let _this = this;
            this.$refs.orgtable.$nextTick(() => {
                _this.$refs.orgtable.loadList();
            });
        },
        refresh () {
            this.query = {
                orgName: '',
                startDate: '',
                endDate: ''
            };
            /*this.cityinfo = this.quertCity = {
                province: '',
                city: '',
                block: ''
            };*/
            let _this = this;
            this.$refs.orgtable.$nextTick(() => {
                _this.$refs.orgtable.loadList();
            });
        },
        showAdd () {
            this.$refs.orgtable.showAdd();
        }
    }
};
